<?php
/**
 * <https://y.st./>
 * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'title' => 'sort_fruits.py',
	'subtitle' => 'Written in <span title="Programming Fundamentals">CS 1101</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalized on 2017-03-15',
	'copyright year' => '2017',
	'body' => <<<END
<h2>Flowchart</h2>
<img class="weblog-header-image" src="/img/CC_BY-SA_4.0/y.st./coursework/CS1101/sort_fruits.py.png" alt="A flowchart depicting the flow of the script below" width="693" height="1225" />
<h2>Script:</h2>
<blockquote>
<pre><code># Every good program should begin with a license comment that declares the file
# as being covered by a free software license.
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see &lt;https://www.gnu.org./licenses/&gt;.

input_file = open(&quot;unsorted_fruits.txt&quot;, &quot;r&quot;)

fruits = []

# We could do this by reading the whole file at once and parsing it,
# but the assignment instructions say to use a while loop somewhere in
# our scripts. Using the readline() method gives us an excuse to do
# just that.
line = input_file.readline()

while line:
# The line endings in this file are a mess. We need to clean them. Some
# lines end in &quot;\\r\\n\\n&quot;, some end in &quot;\\n\\r\\n&quot;, and one ends in &quot;\\n&quot;. We
# want them *all* to end in &quot;\\n&quot;.
	line = line.strip() + &quot;\\n&quot;
# Python is confused by the bizarre and inconsistent line endings. I
# can&apos;t blame it, I&apos;m confused by them too. Python&apos;s confusion results
# in the appearance of several blank lines in our data set. Let&apos;s
# ignore those instead of putting them in our list.
	if line != &quot;\\n&quot;:
		fruits.append(line)
# Read the next line for the next iteration.
	line = input_file.readline()

input_file.close()

fruits.sort()

output_file = open(&quot;sorted_fruits.txt&quot;, &quot;w&quot;)

# We could concatenate the fruit lines together and write them all at
# once, but the assignment instructions say to use a for loop somewhere
# in our scripts. Iterating over our array of lines and writing them
# one at a time gives us an opportunity to do that.
for fruit in fruits:
	output_file.write(fruit)

output_file.close()</code></pre>
</blockquote>
<h2>sorted_fruits.txt:</h2>
<blockquote>
<pre>apple
banana
cherry
date
elderberry
fig
grape
huckleberry
imbu
juneberry
kiwifruit
lime
mango
nectarine
orange
papaya
quince
raspberry
strawberry
tamarind
umkolo
vanilla
watermelon
xigua
yiessas
zapote blanco</pre>
</blockquote>
END
);
